package com.example.app.resources;

import com.example.app.domain.entity.Table;
import com.google.common.base.Joiner;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;

@RestController
@Api(tags = "数据查询接口demo", value = "数据获取")
public class DemoResource {

    @Autowired
    JdbcTemplate myH2JdbcTemplate;



    @GetMapping("/select/{sql}")
    @ApiOperation(value = "输入sql语句查询内容", notes = "备注")
    public Object select(@PathVariable String sql) {
        return myH2JdbcTemplate.queryForList(sql);
    }


    @PostMapping("/select")
    @ApiOperation(value = "输入表名和字段查询内容", notes = "备注")
    public Object select(@RequestBody Table table) {
        Joiner joiner = Joiner.on(",");
        String sql = "select " + joiner.join(table.getFileds()) + " from " + table.getTableName();

        return myH2JdbcTemplate.queryForList(sql);
    }

    @GetMapping("/points")
    @ApiOperation(value = "查询示例", notes = "备注")
    public Object points() {
        return myH2JdbcTemplate.queryForList("SELECT * FROM `a1`");
    }
}
